﻿using Test.Site.Data.Constant;
using Test.Utility.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http.ModelBinding;

namespace Test.Site.Data.Result
{
    public static class ResultHelper
    {
        public static ApiJsonResultData GetApiResult<T, TResult>(Func<T, OperateResult<TResult>> getResult, T operateModel, ModelStateDictionary modelState = null)
        {
            ApiJsonResultData result = new ApiJsonResultData();
            result.RunWithTry(data =>
            {
                if (modelState.IsNull() || modelState.IsValid) //校验数据
                {
                    var operateResult = getResult(operateModel); // 获取操作结果
                    result.data = operateResult.Data;
                    result.code = operateResult.Code;
                    result.msg = operateResult.Msg;
                }else
                {
                    result.SetModelValidErrorMessage(modelState);
                }
            });
            return result;

        }

        public static ApiJsonResultData GetMvcApiResult<T, TResult>(Func<T, OperateResult<TResult>> getResult, T operateModel, System.Web.Mvc.ModelStateDictionary modelState = null)
        {
            ApiJsonResultData result = new ApiJsonResultData();
            result.RunWithTry(data =>
            {
                if (modelState.IsNull() || modelState.IsValid)
                {
                    var operateResult = getResult(operateModel);
                    result.data = operateResult.Data;
                    result.code = operateResult.Code;
                    result.msg = operateResult.Msg;
                }
                else
                {
                    result.SetModelValidErrorMessage(modelState);
                }
            });
            return result;

        }
    }
}
